﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Linq;
using OfficeOpenXml;

namespace ToolExcelPub
{
    public static partial class XTool
    {
        public static void OutTsClient()
        {
            if (!OutputTsNeed) return;

            ShowInfo("OutTsClient!");

            string path_class = PathTs + "\\" + "confVue.ts";
            var sbw = new StringBuilder();

            sbw.AppendLine(NoteGenerated);
            sbw.AppendLine($"//引入axios");
            sbw.AppendLine($"import axios from 'axios'");
            sbw.AppendLine($"export namespace conf {{");
            sbw.AppendLine();

            //公共类
            foreach (var item in DictXPclassInfo.Values)
            {
                var sbnode = new StringBuilder();
                sbnode.AppendLine($"\t// {item.Desc}");
                sbnode.AppendLine($"\texport class {item.Name} {{");
                foreach (var item2 in item.ListNode)
                {
                    sbnode.AppendLine($"\t\t// {item2[2]}");
                    var tp = item2[0];
                    (var t, var v) = TsGetTypeValue(tp);
                    sbnode.AppendLine($"\t\tpublic {item2[1]}: {tp} = {v};");
                }
                sbnode.AppendLine($"\t}}");
                sbw.Append(sbnode);
            }

            //客户端类
            foreach (var page in DictPages.Values)
            {
                if (!page.HasType(EToolFileType.客户端))
                {
                    continue;
                }
                var sbnode = new StringBuilder();
                sbnode.AppendLine($"\texport class {page.Name} {{\t// {page.NameFile}");
                for (int iii = 0; iii < page.Head.Count; iii++)
                {
                    var head = page.Head[iii];
                    if (head == "")
                    {
                        continue;
                    }
                    string tp = page.TypeClient[iii];
                    if (string.IsNullOrEmpty(tp))
                    {
                        continue;
                    }
                    //if (!Wd.ClientLangAll)
                    //{
                    //    if (head.IsLang())
                    //    {
                    //        if (head.IsLangCur())
                    //        {
                    //            head = StrLang;
                    //        }
                    //        else
                    //        {
                    //            continue;
                    //        }
                    //    }
                    //}
                    (var t, var v) = TsGetTypeValue(tp);
                    sbnode.AppendLine($"\t\tpublic {head}: {t} = {v}; //{page.HeadC[iii]}");
                }
                sbnode.AppendLine($"\t}}");
                sbw.Append(sbnode);
            }

            //枚举
            {
                Dictionary<string, Dictionary<string, string>> DictDictEnums = new Dictionary<string, Dictionary<string, string>>();
                Dictionary<string, string> DictDictEnumsDesc = new Dictionary<string, string>();
                foreach (var kvp in DictDictEnumPub)
                {
                    DictDictEnums[kvp.Key] = kvp.Value;
                    DictDictEnumsDesc[kvp.Key] = DictDictEnumPubDesc[kvp.Key];
                }
                foreach (var kvp in DictDictEnumPageId)
                {
                    DictDictEnums[kvp.Key] = kvp.Value;
                }
                foreach (var kvp in DictDictEnumPage)
                {
                    DictDictEnums[kvp.Key] = kvp.Value;
                    DictDictEnumsDesc[kvp.Key] = DictDictEnumPubDesc[kvp.Key];
                }
                foreach (var kvp in DictDictEnums)
                {
                    var sbnode = new StringBuilder();
                    if (DictDictEnumsDesc.TryGetValue(kvp.Key, out var enumdesc))
                    {
                        sbnode.AppendLine($"\t//{enumdesc}");
                    }
                    sbnode.AppendLine($"\texport enum E{kvp.Key} {{");
                    long min = 0, max = 0;
                    foreach (var kvp2 in kvp.Value)
                    {
                        sbnode.AppendLine($"\t\t{kvp2.Key} = {kvp2.Value},");
                        long.TryParse(kvp2.Value, out long tempmax);
                        min = Math.Min(min, tempmax);
                        max = Math.Max(max, tempmax);
                    }
                    sbnode.AppendLine($"\t}}");
                    sbw.Append(sbnode);
                }
            }

            //map
            {
                foreach (var page in DictPages.Values)
                {
                    if (!page.HasType(EToolFileType.客户端))
                    {
                        continue;
                    }
                    var sbnode = new StringBuilder();
                    sbnode.AppendLine($"\t//{page.NameCn}\t// {page.NameFile}");
                    var confname = page.Name.Replace("Config", "Conf");
                    //var keytype = nn.TypeClient[0];
                    var keytype = "string";
                    sbnode.AppendLine($"\texport let {confname} = new Map<{keytype}, {page.Name}>();");
                    sbw.Append(sbnode);
                }
            }

            //read
            {
                sbw.AppendLine();
                sbw.AppendLine($"\texport function InitConfRead() {{");
                sbw.AppendLine($"\t\tconsole.log('InitConfRead')");

                foreach (var page in DictPages.Values)
                {
                    if (!page.HasType(EToolFileType.客户端))
                    {
                        continue;
                    }
                    var sbnode = new StringBuilder();
                    sbnode.AppendLine($"\t\t//{page.NameCn}");
                    var confname = page.Name.Replace("Config", "Conf");
                    sbnode.AppendLine($"\t\taxios.get(\"/confjson/{page.Name}.json\").then((data) => {{ for (let i in data.data) {{ {confname}.set(i, data.data[i]); }} }})");
                    sbw.Append(sbnode);
                }
                sbw.AppendLine($"\t}}");
            }

            //read2
            {
                sbw.AppendLine();
                sbw.AppendLine($"\texport function InitConfRead2() {{");
                sbw.AppendLine($"\t\tconsole.log('InitConfRead2')");

                foreach (var page in DictPages.Values)
                {
                    if (!page.HasType(EToolFileType.客户端))
                    {
                        continue;
                    }
                    var sbnode = new StringBuilder();
                    sbnode.AppendLine($"\t\t//{page.NameCn}");
                    var confname = page.Name.Replace("Config", "Conf");
                    sbnode.AppendLine($"\t\taxios.get(\"/confjson/{page.Name}.json\").then((data) => {{ for (let i in data.data) {{ {confname}.set(i, data.data[i]); }} }})");
                    sbw.Append(sbnode);
                }
                sbw.AppendLine($"\t}}");
            }

            sbw.AppendLine();
            sbw.AppendLine($"}}");

            ToolGlobal.WriteNewFile(path_class, sbw.ToString());
        }
        public static (string, string) TsGetTypeValue(string tp)
        {
            string t = tp;
            string v = "any";
            switch (tp)
            {
                case "bool":
                    t = "boolean";
                    v = "false";
                    break;
                case "string":
                    v = "''";
                    break;
                case "int":
                case "long":
                case "float":
                case "double":
                    t = "number";
                    v = "0";
                    break;

                case "List<string>":
                    t = "string[]";
                    v = "[]";
                    break;
                case "List<int>":
                case "List<long>":
                case "List<float>":
                case "List<double>":
                    t = "number[]";
                    v = "[]";
                    break;
                case "List<List<string>>":
                    t = "string[][]";
                    v = "[]";
                    break;
                case "List<List<int>>":
                    t = "number[][]";
                    v = "[]";
                    break;
                case "Dictionary<string,string>":
                case "Dictionary<int,int>":
                case "Dictionary<long,long>":
                case "Dictionary<string, string>":
                case "Dictionary<int, int>":
                case "Dictionary<long, long>":
                    t = "Map";
                    v = "0new Map()";
                    break;
                default:
                    {
                        var tpClient = tp;
                        if (DictXPclassInfo.ContainsKey(tpClient))
                        {
                            v = $"new {tpClient}";
                        }
                        else
                        {
                            if (tpClient.StartsWith("List<"))
                            {
                                var pclassname = tpClient.GetMidStr("List<", ">");
                                if (DictXPclassInfo.ContainsKey(pclassname))
                                {
                                    t = $"{pclassname}[]";
                                    v = "[]";
                                }
                            }
                        }
                    }
                    break;
            }
            return (t, v);
        }

    }
}
